<template>
  <div class="login_box">
    <div class="center_box">
      <div class="top_div">
        <img src="../plugins/123456.jpeg" />
      </div>
      <!-- 表单 -->
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="loginin_form"
      >
        <el-form-item prop="username">
          <el-input v-model="ruleForm.username" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item class="form_bottom">
          <el-button type="primary" @click="login_in">登录</el-button>
          <el-button type="info" @click="reset_Form">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "VueShopsLogin",

  data() {
    return {
      ruleForm: {
        password: "123456",
        username: "admin",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 5, max: 8, message: "长度在 5 到 8 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 5, max: 8, message: "长度在 5 到 8 个字符", trigger: "blur" },
        ],
      },
    };
  },

  mounted() {},

  methods: {
    // 登录
    login_in() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          const data = await this.axios.post("/login", this.ruleForm);
          console.log(data.data.data);
          if (data.data.meta.status === 200) {
            sessionStorage.token = data.data.data.token;
            this.$message.success("登录成功");
            this.$router.push("/home");
          } else {
            this.$message.error("登录失败");
          }
        } else {
          this.$message.error("用户名或密码不符合规则，请重新输入");
        }
      });
    },
    // 重置
    reset_Form() {
      this.$refs.ruleForm.resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.login_box {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
  position: relative;
  .center_box {
    width: 450px;
    height: 300px;
    background-color: rgb(255, 253, 249);
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 10px;
    box-shadow: 0 0 10px #ddd;
    .top_div {
      width: 140px;
      height: 140px;
      border-radius: 50%;
      background-color: white;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      box-shadow: 0 0 10px #ddd;
      padding: 10px;
      border: 1px solid rgb(241, 241, 241);
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .loginin_form {
      width: 100%;
      position: absolute;
      top: 70%;
      left: 40%;
      transform: translate(-50%, -50%);
      .form_bottom {
        float: right;
      }
    }
  }
}
</style>